<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../../css/mui.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="../../css/header.css" />

		<style>

			#scanComponent {
				width: 100%;
				top: 44px;
				bottom: 0px;
				position: absolute;
				text-align: center;
				background: #000000;
			}
			
		</style>
	</head>

	<body>

		<header class="mui-bar mui-bar-nav title" style="position: fixed;">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left title-color"></a>
			<h1 class="mui-title title-color"><b id="chatting-nickname">扫一扫</b></h1>
		</header>

		<div id="scanComponent"></div>

		<script src="../../js/mui.js"></script>
		<script type="application/javascript" src="../../js/app.js"></script>
		<script type="text/javascript">
			let scan = null;

			mui.init();

			mui.plusReady(function() {
				// 获取当前页面
				let currentWebview = plus.webview.currentWebview();
				currentWebview.addEventListener("show", function() {
					console.log(app.formatDate(new Date()) + ': [info]' + ' ======> [开始扫描...]');
					setTimeout(startScan, "300");
				});
			});

			function startScan() {
				let styles = {
					frameColor: "#128E12",
					scanbarColor: "#0062CC",
					background: ""
				}

				scan = new plus.barcode.Barcode('scanComponent', null, styles);
				scan.onmarked = onmarked;
				scan.start();
			}

			function onmarked(type, result) {
				if (type === 0) {
					scan.close(); // 关闭扫描
					if (!result || !result.startsWith("account=")) {
						mui.alert(result, ' ', '确定', function(e) {
							setTimeout(startScan, 100);
						}, 'div')
					} else {
						console.log(app.formatDate(new Date()) + ': [info]' + ' ======> [扫描完成]: result = ' + result);
						let acount = result.split(",")[0];
						plus.nativeUI.showWaiting("请稍后...");
						mui.ajax(app.serverUrl + '/friend/search?' + acount, {
							data: {
								userId: app.getUserId()
							},
							dataType: 'json', //服务器返回json格式数据
							type: 'get', //HTTP请求类型
							timeout: 10000, //超时时间设置为10秒；
							headers: {
								'Content-Type': 'application/x-www-form-urlencoded',
								'auth-token': app.getAuthToken()
							},
							success: function(res) {
								//服务器返回响应
								plus.nativeUI.closeWaiting();
								if (res.code == 0) {
									console.log(app.formatDate(new Date()) + ': [info]' + ' ======> [好友资料]: ' + JSON.stringify(res.data));
									plus.webview.close(plus.webview.currentWebview());
									setTimeout(() => {
										mui.openWindow({
											url: "friend-detail.html",
											id: "friend-detail.html",
											styles: {},
											extras: {
												requestFriend: res.data
											}
										});
									}, 100);
								} else {
									app.showToast(res.msg, "error");
									setTimeout(startScan, 100);
								}
							},
							error: function(xhr, type, errorThrown) {
								app.showToast(type, "error"); //异常处理；
							}
						});
					}
				}
				scan.start();
			}
		</script>
	</body>

</html>
